<?php
class Customer_Model_Customer extends Zend_Db_Table_Abstract
{	
	/**
	 * Model name
	 * @var string
	 */
	protected $_name = 'customer';
	
	/**
	 * Primary column
	 * @var int
	 */
	protected $_primary = 'customer_id';

	/**
	 * Create customer
	 * @param string $name
	 * @param string $email
	 * @param string $ssn
	 * @param string $mobile
	 * @return int
	 */
	public function createCustomer($name = 'Khách hàng không tên', $email, $ssn, $mobile)
	{	
		// generate customer name and insert new customer
		$customerRow = $this->createRow();
		$customerRow->customer_name = $name;
		$customerRow->email = $email;
		$customerRow->ssn = $ssn;
		$customerRow->mobile = $mobile;
		return $customerRow->save();
	}
	
	/**
	 * Get customer name by user ID
	 * @param int $userID
	 * @return string
	 */
	public function getCustomerNameByUserID($userID)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$queryString = "SELECT customer_name FROM customer WHERE customer_id IN (SELECT customer_id FROM users WHERE user_id = ?)";
		$stmt = $db->query($queryString, $userID);
		$result = $stmt->fetchAll();
		return empty($result) ? null : $result[0]['customer_name'];
	}
	
	/**
	 * Get customer name by username
	 * @param string $username
	 * @return string
	 */
	public function getCustomerNameByUsername($username)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$queryString = "SELECT customer_name FROM customer WHERE customer_id IN (SELECT customer_id FROM users WHERE username = ?)";
		$stmt = $db->query($queryString, $username);
		$result = $stmt->fetchAll();
		return empty($result) ? null : $result[0]['customer_name'];		
	}
	
}